package cn.com.woxitech.dev.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

import cn.com.woxitech.dev.common.Const;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
	@Autowired
	private Environment env;
	
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        //subscribe
    	//   ---> /ws --> /ws/room/{roomid}
    	//   ---> /ws --> /ws/room/{roomid}/user/{userid}
    	//	 ---> /ws --> /ws/user/{userid}/self/{selfid}
    	config.enableSimpleBroker("/ws");
        
    	//send
    	//   ---> /appchat/room/{roomid}/fromuser/{fromuserid}
    	//	 ---> /appchat/room/{roomid}/fromuser/{fromuserid}/touser/{touserid}
        config.setApplicationDestinationPrefixes("/appchat");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
    	
    	String domain = env.getProperty("testorproduction.crossdomain");
        registry.addEndpoint("/appchat_websocket").setAllowedOrigins("*").withSockJS();
    }

}